#include "printtool.h"
#include "ui_printtool.h"

PrintTool::PrintTool(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::PrintTool)
{
    ui->setupUi(this);
}

PrintTool::~PrintTool()
{
    delete ui;
}

void PrintTool::printEvent(QPixmap printPixmap, QWidget *parentWidget){
    this->printPixmap = printPixmap;
    this->parentWidget = parentWidget;

    QPrinter printer(QPrinter::HighResolution);
    //自定义纸张大小
    printer.setPageSize(QPrinter::Custom);
    printer.setPaperSize(QSizeF(printPixmap.height(), printPixmap.width()),
                               QPrinter::Point);
    QPrintPreviewDialog preview(&printer, parentWidget);
    preview.setMinimumSize(1000,600);
    connect(&preview, SIGNAL(paintRequested(QPrinter*)), SLOT(printPreviewSlot(QPrinter*)));
    preview.exec ();
}




void PrintTool::printPreviewSlot(QPrinter* printerPixmap)
{
    printerPixmap->setOrientation(QPrinter::Landscape);
    //获取界面的图片
    QPixmap pixmap = this->printPixmap;
    QPainter painterPixmap(this);
    painterPixmap.begin(printerPixmap);
    QRect rect = painterPixmap.viewport();
    int x = rect.width() / pixmap.width();
    int y = rect.height() / pixmap.height();
    painterPixmap.scale(x, y);
    painterPixmap.drawPixmap(0, 0, pixmap);
    painterPixmap.end();
}
